package sonia.test;

import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import rlm.RuleLayoutManager;
import alm.ALMException;

public class MainFrame extends JFrame{
	private JPanel panel;
	private JEditorPane ruleText;
	private JButton addPanel;
	private JButton execute;
	private int current;
	private String layoutFile;
	
	private File currentFile = null;
	private LayoutManager layout;

	public MainFrame() throws ALMException{
		super("Layout testing");
		current = 1;
		setDefaults();
		createLayout();
		addActions();
		
	}

	private void setDefaults() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setSize(300,400);
		this.setLocation(300, 200);
	}

	public static void main(String[] args) throws ALMException{
		//MainFrame frame = new MainFrame();
		
		boolean[][] arr = new boolean[7][6];
		arr[3] = new boolean[9];
		
		System.out.println(arr[0].length);
		System.out.println(arr[3].length);

//		// add 5 components
//		for(int i = 0; i < 7; i ++)
//			frame.addButton();
//		
//		frame.setVisible(true);
	}
	
	private void createLayout() throws ALMException {
		layout = new RuleLayoutManager("src/MainFrame.drl");
		this.setLayout(layout);
		
		// add main menu
		this.setJMenuBar(new MyMenu());
		
		// add rule and view components
		ruleText = new JEditorPane();
		JScrollPane pane = new JScrollPane(ruleText);
		pane.setName("text");
//		this.add(pane);
		
		addPanel = new JButton("add panel");
		addPanel.setName("add");
		this.add(addPanel);
		
		execute = new JButton("excecute");
		execute.setName("execute");
		this.add(execute);
		
		createMainPanel();
	}
	
	public void addButton(){
		JButton newButton = new JButton(generatName());
		panel.add(newButton);
		panel.invalidate();
		panel.doLayout();
	}
	
	private void createMainPanel() throws ALMException {
		// create layout file
		layoutFile = "src/flow.drl";

		panel = new JPanel();
		panel.setName("panel");
		this.add(panel);
		panel.setLayout(new RuleLayoutManager(new FlowLayout(), new String[0]));
	}

	private void addActions(){
		ActionListener addAction = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				addButton();
			}
		};
		addPanel.addActionListener(addAction);
		
		execute.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					ruleText.write(new FileWriter(layoutFile));
				} catch (IOException e1) {
					System.out.println("could not write rule file");
					e1.printStackTrace();
				}
				
			}
		});
		
	}
	
	private void setCurrentFile(String filename){
		File file = new File(filename);
		try {
			boolean yes = file.createNewFile();
			currentFile = file;
			FileInputStream in = new FileInputStream(file.getAbsolutePath());
			ruleText.read(in, null);
		} catch (IOException e) {
			System.out.println("error creating layout file");
			e.printStackTrace();
		}
	}
	
	private String generatName(){
		String name = ""+current++;
		return name;
	}
	
	class MyMenu extends JMenuBar{
		JMenu menu;
		
		public MyMenu(){
			menu = new JMenu("File");
			menu.add(new AbstractAction(){
				{
					putValue(AbstractAction.NAME, "Load File");
					putValue(AbstractAction.SHORT_DESCRIPTION, "Load rules file (drl)");
				}
				
				@Override
				public void actionPerformed(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int result = fc.showOpenDialog(MainFrame.this);
					if (result == JFileChooser.APPROVE_OPTION){
						File file = fc.getSelectedFile();
						RuleLayoutManager lm = (RuleLayoutManager) panel.getLayout();
						lm.loadFile(file.getAbsolutePath());
						
					}
				}
			});

			
			menu.add(new AbstractAction(){
				{
					putValue(AbstractAction.NAME, "Save");
					putValue(AbstractAction.SHORT_DESCRIPTION, "Save rules file (drl)");
				}

				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("unused at the moment");
				}
			});
			
			this.add(menu);
		}
	}
}
